Web API、データベースからのデータの取得
Web APIを用いたデータの取得
code: Python
import requests
resp = requests.get(url)
resp
data = resp.json()
--------------------------------------------------------------------------
'REF/TST: replace capture_stderr with pytest capsys fixture'
--------------------------------------------------------------------------
code: Python
issues
--------------------------------------------------------------------------
number title labels state
0 24496 REF/TST: replace capture_stderr with pytest ca... [] open
1 24495 BUG: fix .iat assignment creates a new column [] open
2 24494 BUG-19214 int categoricals are formatted as ints [] open
3 24493 ENH: Add additional options to nonexistent in... [{'id': 76812, 'node_id': 'MDU6TGFiZWw3NjgxMg=... open
4 24492 TST: Skip db tests unless explicitly specified... [{'id': 127685, 'node_id': 'MDU6TGFiZWwxMjc2OD... open
... ... ... ... ...
25 24435 DataFrame constructor silently ignores dtype... [{'id': 31404521, 'node_id': 'MDU6TGFiZWwzMTQw... open
26 24434 BUG: Pandas any() returning false with true va... [{'id': 76811, 'node_id': 'MDU6TGFiZWw3NjgxMQ=... open
27 24433 Issue warning in slow ExtensionArray base methods [{'id': 849023693, 'node_id': 'MDU6TGFiZWw4NDk... open
28 24429 WIP: ensure names are pinned correctly [{'id': 76811, 'node_id': 'MDU6TGFiZWw3NjgxMQ=... open
29 24425 to_sql to postgres database,error shows bug on... [{'id': 307649777, 'node_id': 'MDU6TGFiZWwzMDc... open
30 rows × 4 columns
--------------------------------------------------------------------------
データベースからのデータの取得
code: Python
import sqlite3
query = """
CREATE TABLE test
(a VARCHAR(20), b VARCHAR(20),
c REAL, d INTEGER
);"""
con = sqlite3.connect('mydata.sqlite')
con.execute(query)
con.commit()
data = [('Atlanta', 'Georgia', 1.25, 6),
('Tallahassee', 'Florida', 2.6, 3),
('Sacramento', 'California', 1.7, 5)]
stmt = "INSERT INTO test VALUES(?, ?, ?, ?)"
con.executemany(stmt, data)
con.commit()
cursor = con.execute('select * from test')
rows = cursor.fetchall()
rows
--------------------------------------------------------------------------
[('Atlanta', 'Georgia', 1.25, 6),
('Tallahassee', 'Florida', 2.6, 3),
('Sacramento', 'California', 1.7, 5)]
--------------------------------------------------------------------------
code: Python
cursor.description
--------------------------------------------------------------------------
(('a', None, None, None, None, None, None),
('b', None, None, None, None, None, None),
('c', None, None, None, None, None, None),
('d', None, None, None, None, None, None))
--------------------------------------------------------------------------
code: Python
pd.DataFrame(rows, columns=[x0 for x in cursor.description]) --------------------------------------------------------------------------
a b c d
0 Atlanta Georgia 1.25 6
1 Tallahassee Florida 2.60 3
2 Sacramento California 1.70 5
--------------------------------------------------------------------------
code: Python
import sqlalchemy as sqla
db = sqla.create_engine('sqlite:///mydata.sqlite')
pd.read_sql('select * from test', db)
--------------------------------------------------------------------------
a b c d
0 Atlanta Georgia 1.25 6
1 Tallahassee Florida 2.60 3
2 Sacramento California 1.70 5
--------------------------------------------------------------------------